import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gzx_dropdown_menu/gzx_dropdown_menu.dart';
import 'package:panda/base/Constant.dart';

class NearbyPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return NearbyFragmentState();
  }
}

class NearbyFragmentState extends State<NearbyPage>
    with WidgetsBindingObserver {
  GZXDropdownMenuController _dropdownMenuController =
      GZXDropdownMenuController();
  List<String> sortList = ["距离", "销量"];
  GlobalKey _stackKey = GlobalKey();
  var _scaffoldKey = new GlobalKey<ScaffoldState>();
  EasyRefreshController _controller = new EasyRefreshController();
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // TODO: implement didChangeAppLifecycleState
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      if (!ModalRoute.of(context).isCurrent) {
        return;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          brightness: Brightness.light,
          backgroundColor: white,
          elevation: 0,
        ),
        preferredSize: Size.fromHeight(0),
      ),
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        child: Stack(
          key: _stackKey,
          children: <Widget>[
            Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(
                      0,
                      ScreenUtil.getInstance().setWidth(DP_20),
                      ScreenUtil.getInstance().setWidth(DP_30),
                      ScreenUtil.getInstance().setWidth(DP_20)),
                  color: white,
                  child: Center(
                    child: new Row(
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setWidth(DP_30),
                              0,
                              0,
                              0),
                          child: Row(
                            children: <Widget>[
                              Image.asset(
                                "images/icon_location.png",
                                scale: 1.2,
                              ),
                              Text('  '),
                              Text(
                                '南京市',
                                style: TextStyle(
                                    color: black_99,
                                    fontSize: ScreenUtil.getInstance()
                                        .setSp(BIG_FONT_SIZE)),
                              )
                            ],
                          ),
                        ),
                        Expanded(
                          flex: 1,
                          child: new GestureDetector(
                            child: Container(
                              padding: EdgeInsets.fromLTRB(
                                  ScreenUtil.getInstance().setHeight(DP_20),
                                  ScreenUtil.getInstance().setWidth(DP_10),
                                  ScreenUtil.getInstance().setHeight(DP_20),
                                  ScreenUtil.getInstance().setWidth(DP_10)),
                              margin: EdgeInsets.fromLTRB(
                                  ScreenUtil.getInstance().setHeight(DP_30),
                                  0,
                                  ScreenUtil.getInstance().setHeight(DP_30),
                                  0),
                              decoration: BoxDecoration(
                                  color: Color.fromARGB(255, 245, 245, 245),
                                  borderRadius: BorderRadius.circular(20.0)),
                              child: new Row(
                                children: <Widget>[
                                  Icon(
                                    Icons.search,
                                    size: ScreenUtil.getInstance()
                                        .setWidth(DP_50),
                                    color: Colors.black45,
                                  ),
                                  Text(
                                    '搜索商家名称',
                                    style: TextStyle(
                                        color: Colors.black45,
                                        fontSize: ScreenUtil.getInstance()
                                            .setSp(NORMAL_FONT_SIZE)),
                                  )
                                ],
                              ),
                            ),
                            onTap: () {
                              print("搜索页面跳转");
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: GestureDetector(
                        child: Container(
                            alignment: Alignment.center,
                            height: ScreenUtil.getInstance().setHeight(DP_60),
                            color: white,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Text('全部',
                                    style: TextStyle(
                                        color: black_99,
                                        fontSize: ScreenUtil.getInstance()
                                            .setSp(SMALL_FONT_SIZE))),
                                Icon(
                                  Icons.arrow_drop_down,
                                  size:
                                      ScreenUtil.getInstance().setHeight(DP_40),
                                  color: black_99,
                                )
                              ],
                            )),
                        onTap: () {
                          setState(() {
                            sortList = ["距离", "销量"];
                            if (_dropdownMenuController.isShow)
                              _dropdownMenuController.hide();
                          });
                        },
                      ),
                    ),
                    Expanded(
                      flex: 2,
                      child: Container(
                        color: white,
                        padding: EdgeInsets.fromLTRB(
                            0, ScreenUtil.getInstance().setHeight(DP_8), 0, 0),
                        height: ScreenUtil.getInstance().setHeight(DP_60),
                        child: GZXDropDownHeader(
                          items: [
                            GZXDropDownHeaderItem(sortList[0]),
                            GZXDropDownHeaderItem(sortList[1]),
                          ],
                          stackKey: _stackKey,
                          controller: _dropdownMenuController,
                          onItemTap: (index) {},
                          height: ScreenUtil.getInstance().setHeight(DP_60),
                          color: white,
                          borderWidth: 1,
                          borderColor: white,
                          dividerColor: white,
                          style: TextStyle(
                              color: black_99,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE)),
                          dropDownStyle: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: mainColor,
                          ),
                          iconSize: ScreenUtil.getInstance().setHeight(DP_40),
                          iconColor: black_99,
                          iconDropDownColor: mainColor,
                        ),
                      ),
                    )
                  ],
                ),
                Expanded(
                  child: EasyRefresh(
                    controller: _controller,
                    enableControlFinishLoad: true,
                    enableControlFinishRefresh: true,
                    child: ListView.builder(
                      itemBuilder: (BuildContext context, int index) {
                        return renderRow(context, index);
                      },
                      itemCount: 5,
                    ),
                    onRefresh: () {
                    },
                    onLoad: () {},
                  ),
                ),
              ],
            ),
            GZXDropDownMenu(
              controller: _dropdownMenuController,
              animationMilliseconds: 0,
              menus: [
                GZXDropdownMenuBuilder(
                    dropDownHeight:
                        ScreenUtil.getInstance().setHeight(DP_60) * 2 + 2,
                    dropDownWidget: new Column(
                      children: <Widget>[
                        Divider(
                          height: 1,
                          color: grey,
                        ),
                        new GestureDetector(
                          onTap: () {
                            setState(() {
                              sortList = ["距离", "销量"];
                              sortList[0] = "距离升序";
                              _dropdownMenuController.hide();
                            });
                          },
                          child: Container(
                            width: double.infinity,
                            height: ScreenUtil.getInstance().setHeight(DP_60),
                            padding: EdgeInsets.all(10),
                            child: Text('距离升序'),
                          ),
                        ),
                        Divider(
                          height: 1,
                          color: grey,
                        ),
                        new GestureDetector(
                          onTap: () {
                            setState(() {
                              sortList = ["距离", "销量"];
                              sortList[0] = "距离降序";
                              _dropdownMenuController.hide();
                            });
                          },
                          child: Container(
                            width: double.infinity,
                            height: ScreenUtil.getInstance().setHeight(DP_60),
                            padding: EdgeInsets.all(10),
                            child: Text('距离降序'),
                          ),
                        ),
                      ],
                    )),
                GZXDropdownMenuBuilder(
                    dropDownHeight:
                        ScreenUtil.getInstance().setHeight(DP_60) * 2 + 2,
                    dropDownWidget: new Column(
                      children: <Widget>[
                        Divider(
                          height: 1,
                          color: grey,
                        ),
                        new GestureDetector(
                          onTap: () {
                            setState(() {
                              sortList = ["距离", "销量"];
                              sortList[1] = "销量升序";
                              _dropdownMenuController.hide();
                            });
                          },
                          child: Container(
                            width: double.infinity,
                            height: ScreenUtil.getInstance().setHeight(DP_60),
                            padding: EdgeInsets.all(10),
                            child: Text('销量升序'),
                          ),
                        ),
                        Divider(
                          height: 1,
                          color: grey,
                        ),
                        new GestureDetector(
                          onTap: () {
                            setState(() {
                              sortList = ["距离", "销量"];
                              sortList[1] = "销量降序";
                              _dropdownMenuController.hide();
                            });
                          },
                          child: Container(
                            width: double.infinity,
                            height: ScreenUtil.getInstance().setHeight(DP_60),
                            padding: EdgeInsets.all(10),
                            child: Text('销量降序'),
                          ),
                        ),
                      ],
                    )),
              ],
            ),
          ],
        ),
        value: SystemUiOverlayStyle.dark,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  renderRow(BuildContext context, int index) {
    return Container(
      padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
      margin: EdgeInsets.fromLTRB(
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          0),
      decoration: BoxDecoration(
          color: white,
          borderRadius:
              BorderRadius.circular(ScreenUtil.getInstance().setHeight(DP_10))),
      child: new Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: CachedNetworkImage(
                imageUrl:
                'https://img14.360buyimg.com/n0/jfs/t1/42163/17/16172/246472/5d8dd42cE12fa785a/efbd7ce9dbaac1c1.jpg',
                width: ScreenUtil.getInstance().setHeight(DP_120),
                fit: BoxFit.fill,
                height: ScreenUtil.getInstance().setHeight(DP_120)),
          ),
          Container(
            width: ScreenUtil.getInstance().setHeight(DP_15),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Expanded(
                      child:  Text('南京总统府(旧址)',
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                        color: Colors.black87)),
                    ),
                     Text('6.84km',
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                        color:mainColor)),
                  ],
                ),
                Container(
                  height: ScreenUtil.getInstance().setHeight(DP_10),
                ),
                Row(
                  children: <Widget>[
                    Image.asset(
                      "images/brand_time.png",
                      scale: 1.3,
                    ),
                    Text('  '),
                    Text('周一至周五  9:00-18:00',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black38)),
                  ],
                ),
                Container(
                  height: ScreenUtil.getInstance().setHeight(DP_10),
                ),
                Row(
                  children: <Widget>[
                    Image.asset(
                      "images/brand_phone.png",
                      scale: 1.3,
                    ),
                    Text('  '),
                    Text('400-2655333',
                        maxLines: 1,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black38)),
                  ],
                ),
                 Container(
                  height: 1,
                  width: double.infinity,
                  margin: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(DP_20), 0, ScreenUtil.getInstance().setHeight(DP_10)),
                  color: f0Color,
                ),
                 Row(
                  children: <Widget>[
                    Image.asset(
                      "images/brand_navigation.png",
                      scale: 1.3,
                    ),
                    Text('  '),
                    Expanded(
                      child: Text('江苏省南京市玄武区长江路292号',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                            color: Colors.black54)),
                    )
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this); //销毁观察者
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}
